package com.yxkj.syh.app.huarong.views;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.syh.app.basic.bindingAdp.edittext.SimpleTextWatcher;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.views.TitleView;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.Goods;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 创建者：SYH
 * 时间：2020/6/8 14:21
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class ReceiveGoodsCheckDialog extends Dialog {

    private Context mContext;
    private OnConfirmClickListener mOnConfirmClickListener;
    private TitleView titleView;
    private TextView tvGoodsName;
    private EditText etPack;
    private EditText etWeight;
    private EditText etKou;
    private EditText etSq;
    private EditText etKouQ;
    private Button btnConfirm;
    private Goods goods;
    private WindowManager.LayoutParams mLayoutParams;

    public ReceiveGoodsCheckDialog(Context context) {
        super(context, R.style.FullDialog);
        //调用顺序很重要
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_receive_goods, (ViewGroup) null);
        titleView = dialogView.findViewById(R.id.titleView);
        titleView.setRightOnClickListener(v -> cancel());
        tvGoodsName = dialogView.findViewById(R.id.tvGoodsName);
        etPack = dialogView.findViewById(R.id.etPack);
        etWeight = dialogView.findViewById(R.id.etWeight);
        etKou = dialogView.findViewById(R.id.etKou);
        etKouQ = dialogView.findViewById(R.id.etKouQ);
        etSq = dialogView.findViewById(R.id.etSq);
        btnConfirm = dialogView.findViewById(R.id.btnConfirm);


        dialogView.findViewById(R.id.tvGoodsName).setOnClickListener(v -> {
            if (mOnConfirmClickListener != null) mOnConfirmClickListener.onSelectGoods();
        });
        dialogView.findViewById(R.id.tvKouPlus).setOnClickListener(v -> {
            etKou.requestFocus();
            kouClick(true);
        });
        dialogView.findViewById(R.id.tvKouReduce).setOnClickListener(v -> {
            etKou.requestFocus();
            kouClick(false);
        });
        dialogView.findViewById(R.id.tvPackPlus).setOnClickListener(v -> {
            packClick(true);
        });
        dialogView.findViewById(R.id.tvPackReduce).setOnClickListener(v -> packClick(false));
        dialogView.findViewById(R.id.tvWeightPlus).setOnClickListener(v -> {
            etWeight.requestFocus();
            weightClick(true);
        });
        dialogView.findViewById(R.id.tvWeightReduce).setOnClickListener(v -> {
            etWeight.requestFocus();
            weightClick(false);
        });
        dialogView.findViewById(R.id.tvKouQPlus).setOnClickListener(v -> {
            etKouQ.requestFocus();
            kouQClick(true);
        });
        dialogView.findViewById(R.id.tvKouQReduce).setOnClickListener(v -> {
            etKouQ.requestFocus();
            kouQClick(false);
        });
        dialogView.findViewById(R.id.tvSqPlus).setOnClickListener(v -> {
            etSq.requestFocus();
            sqClick(true);
        });
        dialogView.findViewById(R.id.tvSqReduce).setOnClickListener(v -> {
            etSq.requestFocus();
            sqClick(false);
        });

        etPack.addTextChangedListener(new SimpleTextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                etPack.setSelection(etPack.getText().length());
            }
        });
        etWeight.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                String editStr = s.toString().trim();

                int posDot = editStr.indexOf(".");
                //不允许输入3位小数,超过三位就删掉
                if (posDot >= 0) {
                    if (editStr.length() - posDot - 1 > 3) {
                        s.delete(posDot + 4, posDot + 5);
                    }
                    if (posDot==0){
                        s.insert(0,"0");
                    }
                }
                etWeight.setSelection(etWeight.getText().length());
                if (etWeight.isFocused()) {
                    etChangedToRecount(0);
                }
            }
        });
        etKou.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                String editStr = s.toString().trim();

                int posDot = editStr.indexOf(".");
                //不允许输入3位小数,超过2位就删掉
                if (posDot >= 0) {
                    if (editStr.length() - posDot - 1 > 3) {
                        s.delete(posDot + 4, posDot + 5);
                    }
                    if (posDot==0){
                        s.insert(0,"0");
                    }
                }


                BigDecimal bp2 = TextUtils.isEmpty(s.toString()) ? BigDecimal.ZERO : new BigDecimal(s.toString());

                if (bp2.compareTo(new BigDecimal(100)) == 1) {//大于100了
                    s.replace(0, s.length(), "100");
                }

                etKou.setSelection(etKou.getText().length());
                if (etKou.isFocused()) {
                    etChangedToRecount(1);
                }
            }
        });
        etKouQ.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                String editStr = s.toString().trim();

                int posDot = editStr.indexOf(".");
                //不允许输入3位小数,超过三位就删掉
                if (posDot >= 0) {
                    if (editStr.length() - posDot - 1 > 3) {
                        s.delete(posDot + 4, posDot + 5);
                    }
                    if (posDot==0){
                        s.insert(0,"0");
                    }
                }

                BigDecimal bp1 = TextUtils.isEmpty(etWeight.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etWeight.getText().toString());
                BigDecimal bp3 = TextUtils.isEmpty(s.toString()) ? BigDecimal.ZERO : new BigDecimal(s.toString());

                if (bp3.compareTo(bp1) == 1) {
                    s.replace(0, s.length(), ComUtil.clearNoUseZero(bp1).toPlainString());
                }

                etKouQ.setSelection(etKouQ.getText().length());
                if (etKouQ.isFocused()) {
                    etChangedToRecount(2);
                }
            }
        });
        etSq.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                String editStr = s.toString().trim();

                int posDot = editStr.indexOf(".");
                //不允许输入3位小数,超过三位就删掉
                if (posDot >= 0) {
                    if (editStr.length() - posDot - 1 > 3) {
                        s.delete(posDot + 4, posDot + 5);
                    }
                    if (posDot==0){
                        s.insert(0,"0");
                    }
                }

                BigDecimal bp1 = TextUtils.isEmpty(etWeight.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etWeight.getText().toString());
                BigDecimal bp4 = TextUtils.isEmpty(s.toString()) ? BigDecimal.ZERO : new BigDecimal(s.toString());

                if (bp4.compareTo(bp1) == 1) {
                    s.replace(0, s.length(), ComUtil.clearNoUseZero(bp1).toPlainString());
                }

                etSq.setSelection(etSq.getText().length());
                if (etSq.isFocused()) {
                    etChangedToRecount(3);
                }
            }
        });


        btnConfirm.setOnClickListener(v -> {
            if (mOnConfirmClickListener != null) {
                if (check()) {
                    cancel();
                    goods.setPackages(Integer.decode(etPack.getText().toString()));//包数
                    goods.setQuantity(new BigDecimal(etWeight.getText().toString()));//供货数量

                    BigDecimal kou = new BigDecimal(etKou.getText().toString());
                    BigDecimal kouQ = new BigDecimal(etKouQ.getText().toString());
                    BigDecimal SQ = new BigDecimal(etSq.getText().toString());
                    goods.setDeduct(kou);
                    goods.setDeductQuantity(kouQ);//设置扣点重量
                    goods.setSettleQuantity(SQ);//结算重量

                    mOnConfirmClickListener.onConfirm(goods);
                }
            }
        });
        setContentView(dialogView);
    }

    private synchronized void etChangedToRecount(int type) { //0 供货重量 1扣点 2扣点重量 3结算重量

        BigDecimal bp1 = TextUtils.isEmpty(etWeight.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etWeight.getText().toString());
        BigDecimal bp2 = TextUtils.isEmpty(etKou.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etKou.getText().toString());
        BigDecimal bp3 = TextUtils.isEmpty(etKouQ.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etKouQ.getText().toString());
        BigDecimal bp4 = TextUtils.isEmpty(etSq.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etSq.getText().toString());

        if (bp1.compareTo(BigDecimal.ZERO) > 0) {//供货重量不为0
            if (type == 0 || type == 1) {
                bp3 = bp1.multiply(bp2.divide(new BigDecimal(100))).setScale(3, RoundingMode.HALF_UP);
                bp4 = bp1.subtract(bp3);
            } else if (type == 2) {
                bp2 = bp3.divide(bp1, 7, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(3, RoundingMode.HALF_UP);
                bp4 = bp1.subtract(bp3);
            } else if (type == 3) {
                bp3 = bp1.subtract(bp4);
                bp2 = bp3.divide(bp1, 7, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(3, RoundingMode.HALF_UP);
            }
        } else {
            bp1 = BigDecimal.ZERO;
            bp2 = BigDecimal.ZERO;
            bp3 = BigDecimal.ZERO;
            bp4 = BigDecimal.ZERO;
        }

        switch (type) {
            case 0:
                etKou.setText(ComUtil.clearNoUseZero(bp2).toPlainString());
                etKouQ.setText(ComUtil.clearNoUseZero(bp3).toPlainString());
                etSq.setText(ComUtil.clearNoUseZero(bp4).toPlainString());
                break;
            case 1:
                etWeight.setText(ComUtil.clearNoUseZero(bp1).toPlainString());
                etKouQ.setText(ComUtil.clearNoUseZero(bp3).toPlainString());
                etSq.setText(ComUtil.clearNoUseZero(bp4).toPlainString());
                break;
            case 2:
                etWeight.setText(ComUtil.clearNoUseZero(bp1).toPlainString());
                etKou.setText(ComUtil.clearNoUseZero(bp2).toPlainString());
                etSq.setText(ComUtil.clearNoUseZero(bp4).toPlainString());
                break;
            case 3:
                etWeight.setText(ComUtil.clearNoUseZero(bp1).toPlainString());
                etKou.setText(ComUtil.clearNoUseZero(bp2).toPlainString());
                etKouQ.setText(ComUtil.clearNoUseZero(bp3).toPlainString());
                break;
        }

    }

    private boolean check() {
        if (TextUtils.isEmpty(etPack.getText().toString())) {
            Tooast.normalInfo("请输入包数");
            return false;
        } else {
            if (TextUtils.isEmpty(etWeight.getText().toString())) {
                Tooast.normalInfo("请输入重量");
                return false;
            } else {
                BigDecimal bp2 = TextUtils.isEmpty(etKou.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etKou.getText().toString());
                BigDecimal bp3 = TextUtils.isEmpty(etKouQ.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etKouQ.getText().toString());
                BigDecimal bp4 = TextUtils.isEmpty(etSq.getText().toString()) ? BigDecimal.ZERO : new BigDecimal(etSq.getText().toString());
                if (bp2.compareTo(BigDecimal.ZERO) == 0 && bp3.compareTo(BigDecimal.ZERO) == 0 && bp4.compareTo(BigDecimal.ZERO) == 0) {
                    Tooast.normalInfo("扣点率,扣点重量,结算重量任填一项即可");
                    return false;
                } else {
                    BigDecimal packs = new BigDecimal(etPack.getText().toString());
                    BigDecimal weight = new BigDecimal(etWeight.getText().toString());
                    if (packs.compareTo(BigDecimal.ZERO) == 0) {
                        Tooast.normalInfo("包数不能为0");
                        return false;
                    } else if (weight.compareTo(BigDecimal.ZERO) == 0) {
                        Tooast.normalInfo("重量不能为0");
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public ReceiveGoodsCheckDialog build(Goods goods) {
        this.goods = goods;
        tvGoodsName.setText(goods.getName());
        if (goods.getSettleQuantity() == null) {
            etPack.setText(goods.getPackages()+"");
            etWeight.setText(ComUtil.clearNoUseZero(goods.getQuantity()).toPlainString());
            etKou.setText("0");
            etKouQ.setText("0");
            etSq.setText("0");
        } else {
            etPack.setText(goods.getPackages() + "");
            etWeight.setText(ComUtil.clearNoUseZero(goods.getQuantity()).toPlainString());
            etKou.setText(ComUtil.clearNoUseZero(goods.getDeduct()).toPlainString());
            etKouQ.setText(ComUtil.clearNoUseZero(goods.getDeductQuantity()).toPlainString());
            etSq.setText(ComUtil.clearNoUseZero(goods.getSettleQuantity()).toPlainString());
        }
        return this;
    }

    private void packClick(boolean isPlus) {
        BigDecimal packBD;
        if (TextUtils.isEmpty(etPack.getText().toString())) {
            packBD = BigDecimal.ZERO;
        } else {
            packBD = new BigDecimal(etPack.getText().toString());
        }
        if (isPlus) {
            packBD = packBD.add(BigDecimal.ONE);
        } else {
            packBD = packBD.subtract(BigDecimal.ONE);
        }
        if (packBD.compareTo(BigDecimal.ZERO) < 0) {
            packBD = BigDecimal.ZERO;
        }
        etPack.setText(ComUtil.clearNoUseZero(packBD).toPlainString());
    }

    private void weightClick(boolean isPlus) {
        BigDecimal weightBD;
        if (TextUtils.isEmpty(etWeight.getText().toString())) {
            weightBD = BigDecimal.ZERO;
        } else {
            weightBD = new BigDecimal(etWeight.getText().toString());
        }
        if (isPlus) {
            weightBD = weightBD.add(BigDecimal.ONE);
        } else {
            weightBD = weightBD.subtract(BigDecimal.ONE);
        }
        if (weightBD.compareTo(BigDecimal.ZERO) < 0) {
            weightBD = BigDecimal.ZERO;
        }
        etWeight.setText(ComUtil.clearNoUseZero(weightBD).toPlainString());
    }

    private void kouClick(boolean isPlus) {
        BigDecimal weightBD;
        if (TextUtils.isEmpty(etKou.getText().toString())) {
            weightBD = BigDecimal.ZERO;
        } else {
            weightBD = new BigDecimal(etKou.getText().toString());
        }
        if (isPlus) {
            weightBD = weightBD.add(BigDecimal.ONE);
        } else {
            weightBD = weightBD.subtract(BigDecimal.ONE);
        }
        if (weightBD.compareTo(BigDecimal.ZERO) < 0) {
            weightBD = BigDecimal.ZERO;
        }
        etKou.setText(ComUtil.clearNoUseZero(weightBD).toPlainString());
    }

    private void kouQClick(boolean isPlus) {
        BigDecimal weightBD;
        if (TextUtils.isEmpty(etKouQ.getText().toString())) {
            weightBD = BigDecimal.ZERO;
        } else {
            weightBD = new BigDecimal(etKouQ.getText().toString());
        }
        if (isPlus) {
            weightBD = weightBD.add(BigDecimal.ONE);
        } else {
            weightBD = weightBD.subtract(BigDecimal.ONE);
        }
        if (weightBD.compareTo(BigDecimal.ZERO) < 0) {
            weightBD = BigDecimal.ZERO;
        }
        etKouQ.setText(ComUtil.clearNoUseZero(weightBD).toPlainString());
    }

    private void sqClick(boolean isPlus) {
        BigDecimal weightBD;
        if (TextUtils.isEmpty(etSq.getText().toString())) {
            weightBD = BigDecimal.ZERO;
        } else {
            weightBD = new BigDecimal(etSq.getText().toString());
        }
        if (isPlus) {
            weightBD = weightBD.add(BigDecimal.ONE);
        } else {
            weightBD = weightBD.subtract(BigDecimal.ONE);
        }
        if (weightBD.compareTo(BigDecimal.ZERO) < 0) {
            weightBD = BigDecimal.ZERO;
        }
        etSq.setText(ComUtil.clearNoUseZero(weightBD).toPlainString());
    }

    public void setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
        this.mOnConfirmClickListener = onConfirmClickListener;
    }

    public void setNewGoods(Goods goods) {
        this.goods.setName(goods.getName());
        this.goods.setProductId(Long.decode(goods.getCode()));
        this.goods.setProductNo(goods.getVal());

        tvGoodsName.setText(goods.getName());
    }

    public interface OnConfirmClickListener {
        void onConfirm(Goods goods);

        void onSelectGoods();
    }

}
